<template>
    <div>
        <div class="title">
            <van-nav-bar
                title="新建地址"
                left-text="返回"
                left-arrow
                @click-left="onClickLeft"
            />           
        </div>
        <div class="address">
            <van-address-edit
                :area-list="areaList"
                show-set-default="true"
                show-search-result
                :search-result="searchResult"
                :area-columns-placeholder="['省', '市', '区']"
                @save="onSave"
                @change-detail="onChangeDetail"
                @change-default="ChangeDefault"
            />
        </div>
    </div>
</template>

<script setup>

import {post} from '../../utils/request'
import { areaList } from '@vant/area-data';
import { NavBar } from 'vant';
import { AddressEdit } from 'vant';
import { ref } from 'vue';
import { objectEntries } from '@antfu/utils';
//返回上一级
const onClickLeft = () => history.back()
// 地址编辑
const searchResult = ref([]);
const ChangeDefault = (value)=>{
    // console.log(default_set);
}
const onChangeDetail = (val) => {
  if (val) {
    searchResult.value = [
      {
        name: '黄龙万科中心',
        address: '杭州市西湖区',
      },
    ];
  } else {
    searchResult.value = [];
  }
}
let data = {
      "name": "",
      "phone": "",
      "area": "",
      "area_name": "",
      "desc": "",
      "default_set":""
}

//添加地址
const onSave = async(item) => {
    console.log(item);
    data.area=item.areaCode
    data.name =item.name
    data.phone =item.tel
    data.area_name = `${item.province}${item.city}${item.country}`
    data.desc=item.desc
    data.default_set=Number(item.isDefault)
    console.log(data);
    let  res = await  post(`/address`,data)
    console.log(res);

};
</script>

<style lang="scss" scoped>
.title{
    width: 100%;
    height: 0.5rem;
}
.address{
    margin-top: 0.1rem;
    background-color: #F7F8FA;
}
</style>